﻿using System;
using System.Linq;
using System.Data.Entity;
using System.Collections.Generic;
using PharmacyPharnet.Core;
using Pharmacy.Data.Entity;

using Pharmacy.Data;
using Pharmacy.Ultilities;
using Pharmacy.Data.Repositories;
using Pharmacy.Business;
using System.Windows.Forms;
using Pharmacy.ExceptionHandling;
using Pharmacy.Ultilities.Resources;
using System.Data;

namespace PharmacyPharnet.MasterData
{
    public partial class FrmCustomer_List : FrmListBase
    {
        #region Declarations
        Repository<Customer> lstMasterData;
        IObjectContext pharmacyContext;
        CustomerBO customerBO;
        #endregion

        #region Constructors
        public FrmCustomer_List()
        {
            InitializeComponent();
            this.PersistentType = typeof(Customer).Name;
            pharmacyContext = ObjectContextFactory.CreatePharmacyContext(Global.UserName, Global.CurrentConnectionString);

        }
        #endregion

        #region Override Methods
        protected override void PopulateData()
        {
            BindData();
        }

        protected override void InitializeBO()
        {
            if (pharmacyContext == null)
                pharmacyContext = ObjectContextFactory.CreatePharmacyContext(Global.UserName, Global.CurrentConnectionString);
            if (customerBO == null)
                customerBO = new CustomerBO(pharmacyContext);
                    
        }
        #endregion

        #region Custom Methods
        private void BindData()
        {
            lstMasterData = customerBO.ListCustomer();            
            List<Customer> lst = lstMasterData.AsQueryable().ToList();
            grdMasterData.DataSource = null;
            this.grdMasterData.DataSource = lst;
            grvMasterData.BestFitColumns();
        }

        private void RegisterEventhandler()
        {
            btnDelete.Click += new EventHandler(btnDelete_Click);
            btnEdit.Click += new EventHandler(btnEdit_Click);
            //
            //btnNew
            //
            this.btnNew.Click += new EventHandler(btnNew_Click);

            //
            //btnReloadData
            //
            this.btnReloadData.Click += new EventHandler(btnReloadData_Click);

        }
        #endregion        

        #region Event Handlers
        void btnReloadData_Click(object sender, System.EventArgs e)
        {
            try
            {
                ViewManager.CreateWaitDialog();
                ViewManager.SetWaitDialogCaption(SR.Instance.GetString(Constant.ENT_CUSTOMER), "Reloading...");
                BindData();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi xảy ra. Vui lòng liên hệ Người quản trị hệ thống để được hỗ trợ.");
                ExceptionHandler.LogWriter.Write(ex, ExceptionHandler.LOG_CAT_GENERAL);
                this.Close();
            }
            finally {
                ViewManager.CloseWaitDialog();
            }
        }

        void btnNew_Click(object sender, System.EventArgs e)
        {
            try
            {
                FrmCustomer_Detail frm = new FrmCustomer_Detail(Enums.ActionForm.New);
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi xảy ra. Vui lòng liên hệ Người quản trị hệ thống để được hỗ trợ.");
                ExceptionHandler.LogWriter.Write(ex, ExceptionHandler.LOG_CAT_GENERAL);
                this.Close();
            }   
            
            //ViewManager.ShowView(frm);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvMasterData.DataSource != null)
                {
                    int focusRow = grvMasterData.FocusedRowHandle;
                    Customer currentObject = grvMasterData.GetRow(focusRow) as Customer;
                    FrmCustomer_Detail frm = new FrmCustomer_Detail(currentObject, Enums.ActionForm.Edit, pharmacyContext);
                    frm.ShowDialog();
                }
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvMasterData.DataSource != null)
                {
                    int focusRow = grvMasterData.FocusedRowHandle;
                    Customer currentObject = grvMasterData.GetRow(focusRow) as Customer;
                    string messageError = string.Empty;
                    bool result = customerBO.DeleteCustomer(currentObject.ID, out messageError );
                    if (result)
                        btnReloadData.PerformClick();
                    else
                        MessageBox.Show(messageError);
                }
            }
            catch (UpdateException exc)
            {
                ExceptionHandler.LogWriter.Write(exc.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(Pharmacy.Ultilities.Resources.SR.Instance.GetString(Constant.KEY_DELETEOBJECTINTRANSACTION));
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }
        }

        private void FrmCustomer_List_Load(object sender, EventArgs e)
        {
            try
            {
                RegisterEventhandler();
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }            
        }
        #endregion                
    }
}